package com.itheima.mall.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.itheima.mall.pojo.Coupon;
import com.itheima.mall.pojo.CouponProductCategoryRelation;
import com.itheima.mall.pojo.CouponProductRelation;
import org.apache.ibatis.annotations.*;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Mapper
public interface CouponMapper extends BaseMapper<Coupon> {


    //优惠券和产品分类关系存入表中
    @Insert("<script>" +
            "insert into sms_coupon_product_category_relation(id,coupon_id,product_category_id,product_category_name,parent_category_name) values" +
            "<foreach collection='list' item='item' index='index' separator=','>" +
            "(null ,#{item.couponId},#{item.productCategoryId},#{item.productCategoryName},#{item.parentCategoryName})" +
            "</foreach>" +
            "</script>")
    void saveCPCRList(List<CouponProductCategoryRelation> list);

    //优惠券和产品关系存入表中
    @Insert("<script>" +
            "insert into sms_coupon_product_relation(id,coupon_id,product_id,product_name,product_sn) values " +
            "<foreach collection='list' item='item' index='index' separator=','>" +
            "(null,#{item.couponId},#{item.productId},#{item.productName},#{item.productSn})" +
            "</foreach>" +
            "</script>")
    void saveCPRList(List<CouponProductRelation> list);

    @Delete("delete from sms_coupon_product_category_relation where coupon_id = #{id} ")
    void deleteCpcById(Long id);


    @Delete("delete from sms_coupon_product_relation where coupon_id = #{id} ")
    void deleteCpById(Long id);

//    @Update("update sms_coupon_product_category_relation set ")
    @Update("<script>" +
            "<foreach collection='list' item='item' index='index' separator=';'>" +
            "update sms_coupon_product_category_relation set " +
            "coupon_id = #{item.couponId}," +
            "product_category_id = #{item.productCategoryId}," +
            "product_category_name = #{item.productCategoryName}," +
            "parent_category_name = #{item.parentCategoryName} " +
            "where id = #{item.id}" +
            "</foreach>" +
            "</script>")
    void updateCPCRList(List<CouponProductCategoryRelation> list);

    @Update("<script>" +
            "<foreach collection='list' item='item' index='index' separator=';'>" +
            "update sms_coupon_product_relation set " +
            "coupon_id = #{item.couponId}," +
            "product_id = #{item.productId}," +
            "product_name = #{item.productName}," +
            "product_sn = #{item.productSn} " +
            "where id = #{item.id}" +
            "</foreach>" +
            "</script>")
    void updateCPRList(List<CouponProductRelation> list);

    @Select("SELECT * FROM sms_coupon_product_category_relation WHERE coupon_id = #{id}")
    List<CouponProductCategoryRelation> selectCpcById(Long id);

    @Select("SELECT * FROM sms_coupon_product_relation WHERE coupon_id = #{id}")
    List<CouponProductRelation> selectCpById(Long id);

}
